<?php
namespace Teacher\Controller;
use Common\Controller\HomebaseController;
require('./Expand/cos/include.php');
use Qcloud_cos\Auth;
use Qcloud_cos\Cosapi;
class CenterController extends HomebaseController {
    function _initialize() {
		parent::_initialize();
		$this->course_obj = D("Common/Course");
		$this->coursetype_obj = D("Common/Coursetype");
		$this->courseview_obj = D("Common/CourseView");
		$this->user_obj = D("Common/Users");
		$this->label_obj = D("Common/Label");
		$this->section_obj = D("Common/Section");
		$this->usercourse_obj = D("Common/Usercourse");
		$this->material_obj = D("Common/Material");
		$this->teacher_order = D("Common/Teacher_order");
		$this->tixian_obj = D("Common/Tixian");
		$this->application_obj = D("Common/Application");
		$userid=sp_get_current_userid();
		$user=$this->user_obj->where(array('id'=>$userid))->find();
		$avatar=$user['avatar'];
		$this->assign('avatar',$avatar);
		$this->assign('user',$user);
		if($user['user_type']<3){
		 $this->error("您无权操作！");
		}
	}
	public function index(){
		$where['cs_state']=1;
		$where['cs_teacher']=sp_get_current_userid();
		$count=$this->course_obj->where($where)->count();
		$page = $this->page($count, 10);
		$courselist=$this->courseview_obj->where($where)->order(array('listorder','id'=>'desc'))->limit($page->firstRow . ',' . $page->listRows)->select();
	     foreach($courselist as $n=> $val){
			$courselist[$n]['xueyuaunshu']=$this->usercourse_obj->where('course_id=\''.$val['id'].'\'')->count();
		}
		$this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->assign("courselist",$courselist);
		$this->display();
	}
	function add(){
	    $this->_getTree();
		$term_id = intval(I("get.term"));
		$term=$this->course_obj->where("ty_id=$term_id")->find();
		$domain=sp_get_domain();
		$this->assign("term",$term);
		$this->assign("domain",$domain);
		$this->display();
	    
	}
	function add_post(){
	if (IS_POST) {
		    $data = $this->course_obj->create();
			$count=$this->course_obj->count();
			$term_id=$_POST['ty_id'];
			$typedata=$this->coursetype_obj->where("term_id=$term_id")->find();
			$teacherdata=M('application')->where(array('user_id'=>sp_get_current_userid()))->find();
			$data['cs_teacher']=sp_get_current_userid();
			$data['top_id']=$typedata['parent'];
			$data['cs_picture']=$_POST['cs_picture'];
			$data['labelid']=$_POST['labelid'];
			$deta['notice']=$_POST['code'];
			$deta['count']=$_POST['count'];
			$data['cs_addtime']=date('Y-m-d H:i:s');
			$data['cs_state']=1;
			$data['cs_brief']=htmlspecialchars_decode($data['cs_brief']);
			if($teacherdata['state']<1){
				$this->error("您还未通过审核，暂时不能添加课程！");
			}
			if(empty($data['cs_name'])){
				$this->error("请填写课程标题！");
			}
			if(empty($data['ty_id'])){
				$this->error("请选择所属分类！");
			}
			if(empty($data['cs_price'])){
				$this->error("请填写课程价格！");
			}
			if(empty($data['mubiao'])){
				$this->error("请填写学习目标！");
			}
			if(empty($data['shihe'])){
				$this->error("请填写适合人群！");
			}
			if(empty($data['sec_numbers'])){
				$this->error("请填写章节数量！");
			}
			if(empty($data['cs_brief'])){
				$this->error("请填写课程描述！");
			}
			
			
	        if($deta['notice']=='sucess'){
				if ($this->course_obj->add($data)) {
					$this->success("添加成功！",U("Teacher/Center/index"));
				}else{
					$this->error("添加失败！");
				}			
			}else{
				if($count>=$deta['count']){
					$this->error($deta['notice']);
				}else{
					if ($this->course_obj->add($data)) {
					    $this->success("添加成功！",U("Teacher/Center/index"));
				    }else{
					   $this->error("添加失败！");
				    }			
				}
			}
		}
	
	}
	
    private function _getTree(){
		$term_id=empty($_REQUEST['term'])?0:intval($_REQUEST['term']);
		$result = $this->coursetype_obj->order(array("listorder"=>"asc"))->select();
		
		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminCoursetype/add", array("parent" => $r['term_id'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';
			$r['visit'] = "<a href='#'>访问</a>";
			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
			$r['id']=$r['term_id'];
			$r['parentid']=$r['parent'];
			$r['selected']=$term_id==$r['term_id']?"selected":"";
			$array[] = $r;
		}
		$tree->init($array);
		$str="<option value='\$id' \$selected>\$spacer\$name</option>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
	}
    function Labelselect(){
       $c_id=I("get.id");
       $label = $this->label_obj->order(array("id"=>"asc"))->where(array('c_id'=>$c_id))->select();
       $json_string = json_encode($label);
       echo $json_string;
    }
    function addsection(){
        $cs_id=  intval(I("get.cs_id"));
		$cs_data=$this->course_obj->where("id=$cs_id")->find();
		$where['cs_id']=$cs_id;
		$where['type_id']=1;
		$zhang_list=$this->section_obj->where($where)->order("id DESC")->select();
		$this->assign("cs_data",$cs_data);
		$this->assign("cs_id",$cs_id);
		$this->assign("zhang_list",$zhang_list);
		$this->display();
    }
    function addsection_post(){
     if (IS_POST) {
			if ($data=$this->section_obj->create()) {
			     if($data['video_type']==1){
					$data['yun_url']=I('post.upvideo');
				}else{
				    $data['yun_url']=I('post.youku_url');
				    $data['playpass']=I('post.playpass');
				}
				if ($this->section_obj->add($data)!==false) {
					session("access_url",null);
					session("access_name",null);
					$this->success("添加成功！",U("Teacher/Center/sectionlist",array('cs_id'=>$data['cs_id'])));
					
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->section_obj->getError());
			}
		}
    }
    function addzhang(){
        $cs_id=  intval(I("get.cs_id"));
		$this->assign("cs_id",$cs_id);
		$this->display();   
    }
    function addzhang_post(){
        if (IS_POST) {
        	
		    $data['cs_id']=intval(I("post.cs_id"));
		    $data['type_id']=intval(I("post.type_id"));
		    $data['sc_name']=I("post.sc_name");
		    $data['addtime']=I("post.addtime");
		    $data['state']=1;
		    if($data['sc_name']==null){
		       $this->error("请填写章节名称！");
		    }
			$result=$this->section_obj->add($data);
			if ($result) {
				
					$this->success("添加成功！" );
				}else{
					$this->error("添加失败！");
				}			
		}
	}
	function addvideo(){
	  	$this->display();
	}
    public function cos_upload() {
   set_time_limit(0);//防止上传大文件超时
     
    //上传文件的路径
    $srcPath=$_FILES['upvideo']['tmp_name'];
    $bucketName = C('DOMAIN'); //Bucket名称
    $dar=Date('Y').'/'.Date('m'); //以年月为目录
    //查询目录 如果无目录则创建目录
    $path = "/$dar/";
    $ispath=Cosapi::statFolder($bucketName, $path);
    if($ispath['code']!='0'){
        //没有目录先创建
        Cosapi::createFolder($bucketName, $path);
    }
    //cos存储的路径
    $dstPath = $path.$_FILES['upvideo']['name'];
    $_SESSION["access_name"]=$_FILES['upvideo']['name'];
    //上传
    if($_FILES['upvideo']['size'] < 7388608){
        //小于8M
        $arr = Cosapi::upload($srcPath,$bucketName,$dstPath);
    }else{
        //大于8M使用分片上传
        $arr = Cosapi::upload_slice($srcPath, $bucketName, $dstPath);
    }
    if($arr['code']=='0'){//上传成功了
    	$_SESSION["access_url"]=$arr['data']['access_url'];
    	$data['url']=$arr['data']['access_url'];
    	$data['sucess']=0;
      //  return json_encode($data);
        $this->success('上传成功');
    }else{
        $this->error('上传失败');
    }
   }
   function sectionlist(){
        $id=  intval(I("get.cs_id"));
		$cs_name=$this->course_obj->where("id=$id")->getField('cs_name');
		$count=$this->section_obj->where(array('cs_id'=>$id,'type_id'=>0))->count();
		$page = $this->page($count, 10);
		$sc_data=$this->section_obj->where(array('cs_id'=>$id,'type_id'=>0))->limit($page->firstRow . ',' . $page->listRows)->order("id ASC")->select();
		$this->assign("sc_data",$sc_data);
	    $this->assign("cs_id",$id);
	    $this->assign("cs_data",$cs_name);
	    $this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->display();
   }
   function courseedit(){
   	    $id=  intval(I("get.id"));
   	    $cs_date=$this->course_obj->where("id=$id")->find();
   	    $this->assign("cs_date",$cs_date);
        $this->display();
   }
    function courseedit_post(){
    if (IS_POST) {
			$id=intval(I("post.id"));
			$data = $this->course_obj->create(); 
			$data['cs_brief']=htmlspecialchars_decode($data['cs_brief']);
			$result=$this->course_obj->where("id=$id")->save($data);
			if ($result) {
				
					$this->success("编辑成功！",U("Teacher/Center/index"));
				}else{
					$this->error("编辑失败！");
				}			
		}
    }
    function coursedel(){
       if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->course_obj->delete($id)!==false) {
				$this->success("删除成功！",U("Teacher/Center/index"));
			} else {
				$this->error("删除失败！");
			}

		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($this->course_obj->where("id in ($tids)")->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
    }
    function secedit(){
        $id=  intval(I("get.id"));
		$section=$this->section_obj->where("id=$id")->find();
		$cs_name=$this->course_obj->where(array('id'=>$section['cs_id']))->getField('cs_name');
		$cs_id=$this->course_obj->where(array('id'=>$section['cs_id']))->getField('id');
		$course = $this->course_obj->order(array("listorder"=>"asc"))->select();
		$this->assign("course",$course);
		$this->assign("cs_id",$cs_id);
		$this->assign("cs_name",$cs_name);
		$this->assign("section",$section);
		$this->display();
    }
    function secdelete(){
        if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->section_obj->delete($id)!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}

		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($this->section_obj->where("id in ($tids)")->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
    
    }
    function secedit_post(){
       if (IS_POST) {
     		$id=  intval(I("post.id"));
     		$data['is_free']='0';
			if ($data=$this->section_obj->create()) {
				if($data['video_type']==1){
					$data['yun_url']=I('post.yun_url');
				}else{
				    $data['yun_url']=I('post.youku_url');
				    $data['playpass']=I('post.playpass');
				}
				if ($this->section_obj->where("id=$id")->save($data)!==false) {
					$this->success("编辑成功！");
				} else {
					$this->error("编辑失败！");
				}
			} else {
				$this->error($this->section_obj->getError());
			}
		}
    }
    function xueyuanlist(){
    	$id=  intval(I("get.cs_id"));
		$cs_name=$this->course_obj->where("id=$id")->getField('cs_name');
		$count=$this->usercourse_obj->where(array('course_id'=>$id))->count();
		$page = $this->page($count, 10);
		$xueyuan=$this->usercourse_obj->where(array('course_id'=>$id))->limit($page->firstRow . ',' . $page->listRows)->select();
	    foreach($xueyuan as $n=> $val){           
             $urlArr = explode("|",$xueyuan[$n]['studied']);  
             $jnum=count( $urlArr);
             $cs_id=$xueyuan[$n][course_id];
             $znum=$this->section_obj->where(array("cs_id"=>$cs_id))->count();
             $xueyuan[$n]['bili'] = ($jnum/$znum)*100;
             $xueyuan[$n]['jnum']=$jnum;
             $xueyuan[$n]['znum']=$znum;
	      } 
	    $this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage()); 
		$this->assign("xueyuan",$xueyuan);
	    $this->assign("cs_name",$cs_name);
	    $this->assign("cs_id",$id);
		$this->display();
    }
    function material(){
    	$id=  intval(I("get.cs_id"));
		$cs_name=$this->course_obj->where("id=$id")->getField('cs_name');
		$cs_id=$this->course_obj->where(array('id'=>$section['cs_id']))->getField('id');
		$count=$this->material_obj->where("cs_id=$id")->count();
		$page = $this->page($count, 10);
		$ma_data=$this->material_obj->where("cs_id=$id")->order("id ASC")->limit($page->firstRow . ',' . $page->listRows)->order("id ASC")->select();
        foreach($ma_data as $n=> $val){
			$ma_data[$n]['scname']=$this->section_obj->where('id=\''.$val['sc_id'].'\'')->getField('sc_name');
		}
	    $this->assign("ma_data",$ma_data);
        $this->assign("cs_id",$id);
		$this->assign("cs_name",$cs_name);
		$this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->display();
    }
    function materialadd(){
    	$id=  intval(I("get.cs_id"));
    	$sc_id=  intval(I("get.sc_id"));
		$cs_name=$this->course_obj->where("id=$id")->getField('cs_name');
		$cs_id=$this->course_obj->where(array('id'=>$section['cs_id']))->getField('id');
		$this->assign("cs_id",$id);
		$this->assign("sc_id",$sc_id);
		$this->assign("cs_name",$cs_name);
        $this->display();
    }
    function materialadd_post(){
       if (IS_POST) {
			$data = $this->material_obj->create();
			if($data['name']==null) {
				$this->error("资料标题不能为空！");
			}
		    if($data['url']==null) {
				$this->error("下载地址不能为空！");
			}
			$data['downname']=$_SESSION["access_name"];
			$result=$this->material_obj->add($data);
			if ($result) {
				    session("access_url",null);
				    session("access_name",null);
					$this->success("添加成功！",U("Teacher/Center/material",array('cs_id'=>$data['cs_id'])));
				}else{
					$this->error("添加失败！");
				}			
			 
		}
    	
    }
	function materialedit(){
		$id=  intval(I("get.id"));
		$data = $this->material_obj->where(array('id'=>$id))->find();
		$this->assign("data",$data);
		$this->display();
	}
	function materialedit_post(){
		 if (IS_POST) {
			$id = intval(I("POST.id"));
			$material = $this->material_obj->where(array('id'=>$id))->find();
			$data['downname']=$_SESSION["access_name"];
			$data['name']=I("POST.name");
			$data['url']=I("POST.url");
			$result=$this->material_obj->where(array('id'=>$id))->save($data);
			if ($result) {
				    session("access_url",null);
				    session("access_name",null);
					$this->success("添加成功！",U("Teacher/Center/material",array('cs_id'=>$material['cs_id'])));
				}else{
					$this->error("添加失败！");
				}			
			 
		}
		
		
	}
    function materialdel(){
    if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->material_obj->delete($id)!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}

		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($this->material_obj->where("id in ($tids)")->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
    
    }
     function pinglun(){
		$cs_id = intval(I("get.cs_id"));
		$cs_name=$this->course_obj->where("id=$cs_id")->getField('cs_name');
		$count=$this->usercourse_obj->where(array('course_id'=>$cs_id,'pinglun'=>array('neq','')))->count();
		$page = $this->page($count, 10);
		$pinglun=$this->usercourse_obj->where(array('course_id'=>$cs_id,'pinglun'=>array('neq','')))->select();
		$this->assign("pinglun",$pinglun);
		$this->assign("cs_name",$cs_name);
		$this->assign("cs_id",$cs_id);
		$this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->display();
	}
	function isfree(){
	 if(IS_POST){ 
	    $id = (int)$_POST['id']; 
		$data=$this->section_obj->where(array('id'=>$id))->find();
		if($data['is_free']==0){
			$data['is_free']=1;
		}else{
		    $data['is_free']=0;
		}
		$this->section_obj->save($data);
		 $this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		return json_encode($data);
		
	 }
	}
	function  sales(){
	  $userid=sp_get_current_userid();
	  $count1=$this->teacher_order->where(array('u_id'=>$userid))->count();
	  $count2=$this->tixian_obj->where(array('u_id'=>$userid))->count();
      $page1 = $this->page($count1, 10);
      $page2 = $this->page($count2, 10);
	  $sales=$this->teacher_order->where(array('u_id'=>$userid))->limit($page1->firstRow . ',' . $page->listRows)->order("id ASC")->select();
      $tixian=$this->tixian_obj->where(array('u_id'=>$userid))->limit($page2->firstRow . ',' . $page->listRows)->order("id ASC")->select();
	  foreach($sales as $n=> $val){
	  	     $inflownum=$inflownum+$val['money'];
			 $sales[$n]['scname']=$this->course_obj->where('id=\''.$val['c_id'].'\'')->getField('cs_name');
		}
	  foreach($tixian as $n=> $val){
	  	     $tixiannum=$tixiannum+$val['money'];
		}
		$keti=$inflownum-$tixiannum;
	  $this->assign('inflownum',$inflownum); 
	   $this->assign('keti',$keti); 
	  $this->assign('tixian',$tixian); 
	  $this->assign('tixiannum',$tixiannum); 
	  $this->assign('sales',$sales); 
	  $this->assign("Page1", $page1->show('Admin'));
	  $this->assign("current_page",$page1->GetCurrentPage());
	    $this->assign("Page2", $page2->show('Admin'));
	  $this->assign("current_page",$page2->GetCurrentPage());    
	  $this->display();
	}
	function tixian(){
	   
	   $this->display();
	}
	function tixian_post(){
	   if(IS_POST){ 
	   	$userid=sp_get_current_userid();
	    $sales=$this->teacher_order->where(array('u_id'=>$userid))->select();
        $tixian=$this->tixian_obj->where(array('u_id'=>$userid))->select();
        foreach($sales as $n=> $val){
	  	     $inflownum=$inflownum+$val['money'];
		}
	    foreach($tixian as $n=> $val){
	  	     $tixiannum=$tixiannum+$val['money'];
		}
		$keti=$inflownum-$tixiannum;
	      $data= $this->tixian_obj->create();
	      if(empty($data['money'])){
	         $this->error('提现金额不能为空');
	      }
	      if($data['money']<100){
	         $this->error('提现金额必须大于100元');
	      }
	     if($data['money']>$keti){
	         $this->error('账户余额小于你的提款额');
	      }
	      if(empty($data['turename'])){
	         $this->error('真实姓名不能为空');
	      }
	      if(empty($data['count'])){
	         $this->error('提款账户不能为空');
	      }
	      $data['u_id']=sp_get_current_userid();
	      $data['addtime']=date('Y-m-d H:i:s');
	      $data['state']=0;
	      if($this->tixian_obj->add($data)){
	      	$this->success('提交成功');
	      }else{
	        $this->error('提交失败');
	      }
		}
	}
	function upvideo(){
	    $this->display();
	}
}